﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace 路径扫描2._0
{
    public partial class ProxxScan : Form
    {
        public ProxxScan()
        {
            InitializeComponent();
        }
        public Thread th;
        private void button3_Click(object sender, EventArgs e)
        {
            //创建测试来源于 https://www.juliangip.com/help/sdk/http/#csharp
            if (textBox1.Text == string.Empty && textBox2.Text == string.Empty && textBox3.Text == string.Empty && textBox4.Text == string.Empty)
            {
                Console.WriteLine("");
                MessageBox.Show("代理配置信息不能为空");
                return;
            }
            else
            {
                th = new Thread(ProxxScanSet);
                th.IsBackground = false;
                th.Start();
            }
        }
        public void ProxxScanSet()
        {
            try
            {
                // 要访问的目标网页
                string page_url = textBox5.Text;
                // 构造请求
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(page_url);
                request.Method = "GET";
                request.Headers.Add("Accept-Encoding", "Gzip");  // 使用gzip压缩传输数据让访问更快
                string proxy_ip = textBox1.Text;
                int proxy_port = Convert.ToInt32(textBox2.Text);//int类型
                // 用户名密码认证(动态代理/独享代理)
                string username = textBox4.Text;
                string password = textBox3.Text;
                // 设置代理 (开放代理或动态/独享代理&已添加白名单)
                // request.Proxy = new WebProxy(proxy_ip, proxy_port);
                // 设置代理 (动态/独享代理&未添加白名单)
                WebProxy proxy = new WebProxy();
                proxy.Address = new Uri(String.Format("http://{0}:{1}", proxy_ip, proxy_port));
                proxy.Credentials = new NetworkCredential(username, password);
                request.Proxy = proxy;
                // 请求目标网页
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Console.WriteLine((int)response.StatusCode);  // 获取状态码
                Console.WriteLine("创建时间：" + DateTime.Now + "   ------   " + "代理创建成功...");
                                                              // 解压缩读取返回内容
                using (StreamReader reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress)))
                {
                    Console.WriteLine(reader.ReadToEnd());
                    Console.WriteLine("创建时间：" + DateTime.Now + "   ------   " + "代理测试成功...");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("代理连接失败" + "\r\n" + ex);
                Console.WriteLine("错误信息" + "\r\n" + ex);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == string.Empty && textBox2.Text == string.Empty)
            {
                Console.WriteLine("创建时间：" + DateTime.Now + "   ------   " + "代理配置不能为空...");
                MessageBox.Show("代理配置信息不能为空");
                return;
            }
            //执行把数据保存到存储中心
            try
            {
                // 读取数据的方法
                string path = @"data source=WebScanDb\Sec_Webscan.db";
                SQLiteConnection dataConn = new SQLiteConnection(path);
                dataConn.Open();
                if (dataConn.State == ConnectionState.Open)
                {
                    //如果数据库能打开的情况下 把数据存入到数据库中
                    //string sql = "UPDATE Sec_reportset SET (username, repname, time) VALUES('{0}', '{1}', '{2}')";
                    string sql = "update sec_Webscan_Setting set service = '" + textBox1.Text + "', port = '" + textBox2.Text + "', user = '" + textBox4.Text + "', pass = '" + textBox3.Text + "'";
                    // repname = "+ textBox2.Text + " addtime = "+ dateTimePicker1.Text +"
                    //sql = string.Format(sql,textBox1.Text,textBox2.Text,dateTimePicker1.Text);
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    command.ExecuteNonQuery();
                    SQLiteDataAdapter mAdapt = new SQLiteDataAdapter(command);
                    var ss = mAdapt.ToString();
                    //判断是否进行执行 如果执行之后就进行打印
                    if (ss.Length != 1)
                    {
                        Console.WriteLine(DateTime.Now + "   ------   " + "配置保存成功" + "\r\n");
                        MessageBox.Show("配置保存成功！" + "\r\n" + "修改时间：" + DateTime.Now);
                        //修改成功后把以下属性设置flase，只有点击修改的时候方可进行修改
                        textBox1.Enabled = false;
                        textBox2.Enabled = false;
                        textBox3.Enabled = false;
                        textBox4.Enabled = false;
                        button1.Enabled = false;
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("配置保存失败！" + "\r\n" + "修改时间：" + DateTime.Now);
                    //进行打印输出  循环绑定到定时器上 执行N次
                    Console.WriteLine(DateTime.Now + "   ------   " + "配置保存失败" + "\r\n");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("配置保存失败！" + "\r\n" + "修改时间：" + DateTime.Now);
                Console.WriteLine("错误时间" + DateTime.Now + "\r\n" + ex);
                return;
            }
            //保存一次扫描记录

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //点击修改的时候，首先对设置的属性还原
            textBox1.Enabled = true;
            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            button1.Enabled = true;
        }

        private void ProxxScan_Load(object sender, EventArgs e)
        {
            textBox1.Enabled = false;
            textBox2.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            button1.Enabled = false;
            //if(textBox1.SelectionLength >= 1)
            //{
            //    textBox1.Enabled = false;
            //}
            try
            {
                // 读取数据的方法
                string path = @"data source=WebScanDb\Sec_Webscan.db";
                SQLiteConnection dataConn = new SQLiteConnection(path);
                dataConn.Open();
                if (dataConn.State == ConnectionState.Open)
                {
                    DataSet ds = new DataSet();
                    string sql = "SELECT service, port, user, pass FROM sec_Webscan_Setting";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    command.ExecuteNonQuery();
                    SQLiteDataAdapter mAdapt = new SQLiteDataAdapter(command);
                    mAdapt.Fill(ds);
                    var ss = mAdapt.ToString();
                    //判断是否进行执行 如果执行之后就进行打印
                    if (ss.Length != 1)
                    {
                        textBox1.Text = ds.Tables[0].Rows[0]["service"].ToString();
                        textBox2.Text = ds.Tables[0].Rows[0]["port"].ToString();
                        textBox3.Text = ds.Tables[0].Rows[0]["pass"].ToString();
                        textBox4.Text = ds.Tables[0].Rows[0]["user"].ToString();
                        Console.WriteLine(DateTime.Now + "   ------   " + "代理配置加载成功" + "\r\n");
                        dataConn.Close();
                    }
                }
                else
                {
                    //进行打印输出  循环绑定到定时器上 执行N次
                    Console.WriteLine(DateTime.Now + "   ------   " + "配置保存失败" + "\r\n");
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("错误时间" + DateTime.Now + "\r\n" + ex);
                return;
            }
        }
    }
}
